<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="产品规格及名称" prop="cpggjmc" label-width="120px">
        <el-input
          v-model="queryParams.cpggjmc"
          placeholder="请输入产品规格及名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="scrq">
        <el-date-picker  :editable="false"
          clearable
          size="small"
          v-model="queryParams.scrq"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择生产日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Productionline_sensory_main:sensory_main:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Productionline_sensory_main:sensory_main:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Productionline_sensory_main:sensory_main:remove']"
          >删除</el-button
        >
      </el-col>
     <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Productionline_sensory_main:sensory_main:export']"
          >导出</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-s-check"
          size="mini"
          :disabled="multiple"
          @click="handleCheck"
          v-hasPermi="['Productionline_sensory_main:sensory_main:check']"
          >审核</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sensory_mainList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="50px" />
      <el-table-column
        label="产品规格及名称"
        align="center"
        prop="cpggjmc"
        width="500"
      />
      <el-table-column label="生产日期" align="center" prop="scrq" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="取样时间" align="center" prop="qyrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qyrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="瓶盖厂家" align="center" prop="gcj" />
      <el-table-column label="瓶坯厂家" align="center" prop="pcj" /> -->
      <!--  <el-table-column label="气味" align="center" prop="qw" />
      <el-table-column label="口感" align="center" prop="kg" />
      <el-table-column label="颜色" align="center" prop="ys" />
      <el-table-column label="肉眼可见物" align="center" prop="rykjw" /> -->
      <el-table-column label="报表编号" align="center" prop="bbbh" />
      <el-table-column label="确认人" align="center" prop="qrrPerson" />

      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="250px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
          >导出</el-button            >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Productionline_sensory_main:sensory_main:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Productionline_sensory_main:sensory_main:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成品矿泉水感官检验确认对话框 -->
    <el-dialog v-myFullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      custom-class="myDialog"
      fullscreen
    >
      <TableHeader
        @nameChange="nameChange"
        :data="{
          mainTitle: '成品矿泉水感官检验确认',
          company: '青岛崂山矿泉水有限公司',
          code: form.bbbh,
        }"
      />
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品规格及名称" prop="cpggjmc">
              <!-- <el-input
                v-model="form.cpggjmc"
                placeholder="请输入产品规格及名称"
                :disabled="openDisabled"
              /> -->
              <Dialog
                @dialogChange="dialogChangeFactory($event, 3)"
                :echoValue="form.cpggjmc"
                parameter="rows"
                value="id"
                label="name"
                :multiple="true"
                search="name/code"
                url="/basic/productInfo/listSelect"
                :column="['产成品编号/code', '产成品名称/name']"
                :axiosData="{ typeCode: 'YYTRKQS' }"
                :openDisabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="scrq">
              <el-date-picker  :editable="false"
                clearable
                size="small"
                v-model="form.scrq"
                type="date"
                value-format="yyyy-MM-dd"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取样时间1" prop="qyrq">
              <el-time-picker
                :editable="false"
                clearable
                v-model="form.qyrq"
                type="time"
                format="HH:mm"
                value-format="HH:mm"
                :disabled="openDisabled"
              >
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取样时间2" prop="qyrq2">
              <el-time-picker
                :editable="false"
                clearable
                v-model="form.qyrq2"
                type="time"
                format="HH:mm"
                value-format="HH:mm"
                :disabled="openDisabled"
              >
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="瓶盖厂家" prop="gcj">
              <!-- <el-input
                v-model="form.gcj"
                placeholder="请输入盖厂家"
                :disabled="openDisabled"
              />
              :echoValue="scope.row.gcj"
               -->
               <!-- <ReportDialog
                @dialogChange="form.gcj = $event.value"
                :echoValue="form.gcj"
                value="id"
                label="label"
                placeholder="选择设备厂家"
                :openDisabled="openDisabled"
              /> -->

              <treeselect
                v-model="form.gcjArray"
                :disable-branch-nodes="true"
                :options="dialogData"
                :limit="2"
                placeholder="选择设备厂家"
                :normalizer="normalizer2"
                :searchable="false"
                :appendToBody="false"
                multiple
                 @select="addSelect"
                :disabled="openDisabled"
                collapse-tags
              />
              <!-- @select="addSelect" -->

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="瓶坯厂家" prop="pcj">
              <!-- <el-input
                v-model="form.pcj"
                placeholder="请输入胚厂家"
                :disabled="openDisabled"
              /> -->

              <!-- <template slot-scope="scope"> -->
              <!-- <Dialog
                @dialogChange="dialogChangeFactory($event, 2)"
                :echoValue="form.pcj"
                parameter="rows"
                value="id"
                label="name"
                :multiple="true"
                search="name/code"
                url="/basic/eqpfactory/list"
                :column="['产成品编号/code', '产成品名称/name']"
                placeholder="请选择内容"
                :axiosData="{ type: 4 }"
              /> -->
              <!-- <ReportDialog
                @dialogChange="form.pcj = $event.value"
                :echoValue="form.pcj"
                value="id"
                label="label"
                placeholder="选择设备厂家"
                :openDisabled="openDisabled"
              /> -->
              <treeselect
                v-model="form.pcjArray"
                :disable-branch-nodes="true"
                :options="dialogData"
                :limit="2"
                placeholder="选择设备厂家"
                :normalizer="normalizer2"
                :searchable="false"
                :appendToBody="false"
                multiple
                 @select="addSelect"
                :disabled="openDisabled"
                collapse-tags
              />
              <!-- </template> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="气味" prop="qw">
              <!-- <el-input v-model="form.qw"
                   placeholder="请输入气味" :disabled="openDisabled" /> -->
              <el-select v-model="form.qw" :disabled="openDisabled">
                <el-option value="正常">正常</el-option>
                <el-option value="否">否</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="口感" prop="kg">
              <!-- <el-input v-model="form.kg" placeholder="请输入口感" :disabled="openDisabled" /> -->
              <el-select v-model="form.kg" :disabled="openDisabled">
                <el-option value="正常">正常</el-option>
                <el-option value="否">否</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="颜色" prop="ys">
              <!-- <el-input v-model="form.ys" placeholder="请输入颜色" :disabled="openDisabled" /> -->
              <el-select v-model="form.ys">
                <el-option value="正常">正常</el-option>
                <el-option value="否">否</el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="肉眼可见物" prop="rykjw">
              <el-select v-model="form.rykjw" :disabled="openDisabled">
                <el-option value="有">有</el-option>
                <el-option value="无">无</el-option>
              </el-select>
              <!-- <el-input v-model="form.rykjw" placeholder="请输入肉眼可见物" :disabled="openDisabled" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
                <el-form-item label="报表编号" prop="bbbh">
                  <el-input v-model="form.bbbh" placeholder="请输入报表编号" :disabled="openDisabled" />
                </el-form-item>
            </el-col> -->

          <el-col :span="12">
            <el-form-item label="确认人" prop="qrrPerson">
              <el-input
                v-model="form.qrrPerson"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSelect2 } from "@/api/report/process/label";
import {
  listSensory_main,
  getSensory_main,
  delSensory_main,
  addSensory_main,
  updateSensory_main,
} from "@/api/quality/productionline/sensory_main";
import { getInfo } from "@/api/login";
import { listUser } from "@/api/system/user";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Sensory_main",
  components: { Treeselect,  },
  data() {
    return {
      /* 当前用户名 */
      nickName: "",
      userList: [],
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      dialogData: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成品矿泉水感官检验确认表格数据
      sensory_mainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        cpggjmc: null,
        scrq: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getListfac();
    this.getList();
    this.queryUserList();
    
  },
  methods: {
    /** 转换数据结构 */
    normalizer2(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label,
        children: node.children,
      };
    },
     addSelect(value){

        console.log(value);
       // this.selectList = value.label

    },
    getListfac() {
      listSelect2().then((response) => {
        this.dialogData = response.rows;
        // console.log(response.rows)
        // // console.log(this.dialogData);
        // /*        this.selectionLabel = this.findPathByLeafId(
        //   this.echoValue,
        //   this.tableData
        // );*/
      });
    },
    /** 查询成品矿泉水感官检验确认列表 */
    getList() {
      this.loading = true;
      listSensory_main(this.queryParams).then((response) => {
        this.sensory_mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    dialogChangeFactory(event, flag) {
      // console.log(this.form);
      if(flag==1){
        this.form.gcj=event.label;
      }
      if(flag==2){
        this.form.pcj=event.label;
      }
      if(flag==3){
        this.form.cpggjmc=event.label;
      }
      // console.log(event);
      // this.rptProLabelCheckList[scope.$index].provider = event.label;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cpggjmc: null,
        scrq: null,
        qyrq: null,
        qyrq2: null,
        gcj: null,
        gcjArray:[],
        pcj: null,
        pcjArray:[],
        qw: "正常",
        kg: "正常",
        ys: "正常",
        rykjw: "无",
        qrrPerson: null,
        bbbh: "LK-QF-LS1-10-015-11-05",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加成品矿泉水感官检验确认";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getSensory_main(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改成品矿泉水感官检验确认";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getSensory_main(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看成品矿泉水感官检验确认";
      });
    },

    excelhandleView(row){
      this.download(
        "Productionline/sensory_main/exportexcel?id="+row.id,
        {
          ...this.queryParams,
        },
        "成品矿泉水感官检验确认" + this.parseTime(new Date()) + ".xlsx"
      );
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {

        if (valid) {
            /**打开遮罩层*/
            const rLoading = this.openLoading();
          if (this.form.id != null) {
            updateSensory_main(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          } else {
            console.log(this.form);
            addSensory_main(this.form)
              .then((response) => {
                /**成功后执行关闭遮罩层*/
                rLoading.close();
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                /**请求异常后执行关闭遮罩层*/
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除成品矿泉水感官检验确认编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delSensory_main(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "Productionline_sensory_main/sensory_main/export",
        {
          ...this.queryParams,
        },
        "成品矿泉水感官检验确认" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.bbbh = e;
    },
    /** 审核按钮操作 */
    handleCheck(row) {
      /* 审核 */
      this.ids.forEach((id) => {
        getSensory_main(id).then((response) => {
          if (
            response.data.qrrPerson != null &&
            response.data.qrrPerson != ""
          ) {
            this.msgError(
              '编号为"' + id + '"的数据项已审核完成，无需重复审核！'
            );
          } else {
            let obj = {};
            obj.id = id;
            obj.qrrPerson = this.nickName;

            /* 修改 */
            updateSensory_main(obj).then((response) => {
              this.msgSuccess('编号为"' + id +'"的数据项审核成功！');
              this.getList();
            });
          }
        });
      });
    },
    /* 查询用户列表 */
    queryUserList() {
      listUser().then((response) => {
        this.userList = response.rows;
      });
      getInfo().then((response) => {
        this.nickName = response.user.nickName;
      });
    },
  },
};
</script>
